/**
 *	@file	bc_key.c
 *	@brief	ぼんぐりケースアプリ 入力取得
 *	@author	Miyuki Iwasawa
 *	@date	08.08.04
 *
 */

#include "common.h"
#include "system/procsys.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/snd_tool.h"
#include "system/fontproc.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/buflen.h"
#include "system/window.h"
#include "system/brightness.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/palanm.h"
#include "system/wipe.h"
#include "system/window.h"
#include "system/font_arc.h"
#include "system/winframe.naix"
#include "gflib/touchpanel.h"

#include "savedata/config.h"
#include "application/bong_case.h"
#include "bc_sys.h"
#include "bc_app.h"
#include "bc_sub.h"
#include "bc_key.h"
#include "bc_snd_def.h"

#ifdef DEBUG_ONLY_FOR_iwasawa
 #undef IWASAWA_PRINT
 #define IWASAWA_PRINT(...) ((void) 0)
#endif

//////////////////////////////////////////////////////////////////////////////
///タッチパネル制御テーブル
static const RECT_HIT_TBL TpMainRect[] = {
//	{0,191,0,255}, ty,by,lx,rx
	{TP_USE_CIRCLE, TP_BONG_X1, TP_BONG01_Y, TP_BONG_R },
	{TP_USE_CIRCLE, TP_BONG_X2, TP_BONG02_Y, TP_BONG_R },
	{TP_USE_CIRCLE, TP_BONG_X1, TP_BONG03_Y, TP_BONG_R },
	{TP_USE_CIRCLE, TP_BONG_X2, TP_BONG04_Y, TP_BONG_R },
	{TP_USE_CIRCLE, TP_BONG_X1, TP_BONG05_Y, TP_BONG_R },
	{TP_USE_CIRCLE, TP_BONG_X2, TP_BONG06_Y, TP_BONG_R },
	{TP_USE_CIRCLE, TP_BONG_X1, TP_BONG07_Y, TP_BONG_R },

	{TP_POT_Y,TP_POT_Y+TP_POT_H,TP_POT_X,TP_POT_X+TP_POT_W},
	{TP_BACK_Y,TP_BACK_Y+TP_BACK_H,TP_BACK_X,TP_BACK_X+TP_BACK_W},
	{RECT_HIT_END,0,0,0}
};

static const RECT_HIT_TBL TpBallNumSetRect[] = {
//	{0,191,0,255}, ty,by,lx,rx
	{TP_BTND_Y,TP_BTND_Y+TP_BTN_H,TP_BTN_X,TP_BTN_X+TP_BTN_W},
	{TP_BTNC_Y,TP_BTNC_Y+TP_BTN_H,TP_BTN_X,TP_BTN_X+TP_BTN_W},
	{TP_AL_Y1,TP_AL_Y1+TP_AL_H,TP_AL_X1,TP_AL_X1+TP_AL_W},
	{TP_AL_Y2,TP_AL_Y2+TP_AL_H,TP_AL_X1,TP_AL_X1+TP_AL_W},
	{TP_AL_Y1,TP_AL_Y1+TP_AL_H,TP_AL_X2,TP_AL_X2+TP_AL_W},
	{TP_AL_Y2,TP_AL_Y2+TP_AL_H,TP_AL_X2,TP_AL_X2+TP_AL_W},
	{RECT_HIT_END,0,0,0}
};

//////////////////////////////////////////////////////////////////////////////
//プロトタイプ
static int exe_NormalMain(BC_APPWORK* wk,u32 code);
static int key_NormalMain(BC_APPWORK* wk);
static int touch_NormalMain(BC_APPWORK* wk,BOOL* exe_f);

static int exe_PotMain(BC_APPWORK* wk,u32 code);
static int key_PotMain(BC_APPWORK* wk);
static int touch_PotMain(BC_APPWORK* wk,BOOL* exe_f);

static int exe_BallMain(BC_APPWORK* wk,u32 code);
static int key_BallMain(BC_APPWORK* wk);
static int touch_BallMain(BC_APPWORK* wk,BOOL* exe_f);

static int exe_BallNumSet(BC_APPWORK* wk,u32 code);
static int ItemNumMove( int num, int max, int move );

static int key_BallNumSet(BC_APPWORK* wk);
static int touch_BallNumSet(BC_APPWORK* wk,BOOL* exe_f);

static BOOL keysub_MainMoveCPos(BC_APPWORK* wk);
static void keysub_NextCPosLineAB(BC_APPWORK* wk,BOOL dir);

/**
 *	@brief	カーソルポジションナンバー取得
 */
u8 BCKey_GetCursorPosNo(BC_APPWORK* wk)
{
	switch(wk->cpos_x){
	case CLINE_A:	//Line A
		IWASAWA_PRINT(" CurPos = %d x = %d, y=%d a=%d, b=%d\n",(wk->cpos_y*2) + CURPOS_BONG01,
			wk->cpos_x,wk->cpos_y,wk->opos_a,wk->opos_b);
		return (wk->cpos_y*2) + CURPOS_BONG01;
	case CLINE_B:	//Line B
		IWASAWA_PRINT(" CurPos = %d x = %d, y=%d a=%d, b=%d\n",(wk->cpos_y*2) + CURPOS_BONG02,
			wk->cpos_x,wk->cpos_y,wk->opos_a,wk->opos_b);
		return (wk->cpos_y*2) + CURPOS_BONG02;
	case CLINE_C:	//Line C
		IWASAWA_PRINT(" CurPos = %d x = %d, y=%d a=%d, b=%d\n",CURPOS_POT,
			wk->cpos_x,wk->cpos_y,wk->opos_a,wk->opos_b);
		if(wk->mode == SYSMODE_POT){
			return CURPOS_POT;
		}else{
			return CURPOS_BACK;
		}
	case CLINE_D:
		IWASAWA_PRINT(" CurPos = %d x = %d, y=%d a=%d, b=%d\n",CURPOS_BACK,
			wk->cpos_x,wk->cpos_y,wk->opos_a,wk->opos_b);
		return CURPOS_BACK;
	}
	return 0;
}

/**
 *	@brief	ノーマルモードメイン選択入力取得
 */
int BCKey_InputNormalMain(BC_APPWORK* wk)
{
	int ret;
	BOOL exe_f = FALSE;

	ret = touch_NormalMain(wk,&exe_f);

	if(exe_f){
		return ret;
	}

	return key_NormalMain(wk);
}
//////////////////////////////////////////////////////////////////////////////
/**
 *	@brief	ポットメイン選択入力取得
 */
int BCKey_InputPotMain(BC_APPWORK* wk)
{
	int ret;
	BOOL exe_f = FALSE;

	ret = touch_PotMain(wk,&exe_f);

	if(exe_f){
		return ret;
	}

	return key_PotMain(wk);
}

/**
 *	@brief	ぼんぐり移動タッチ
 */
int BCKey_InputBongMoveTouch(BC_APPWORK* wk)
{
	s16	x,y;
	u8	pot_in = 0;

	CATS_ObjectPosGet(wk->pAct[ACT_BONG01+wk->target], &x, &y);
	pot_in = GF_TP_SingleHitSelf(&TpMainRect[CURPOS_POT], x, y);

	if(GF_TP_GetCont()){	//継続中
		x = sys.tp_x;
		y = sys.tp_y;

		CATS_ObjectPosSet(wk->pAct[ACT_BONG01+wk->target],x,y);
		CATS_ObjectPosSet(wk->pAct[ACT_CURSOR],x,y-6);

		if(pot_in){
			if(!wk->pot_open_f){
				wk->pot_open_f = TRUE;
				Snd_SePlay( SE_BC_OPEN );
				CLACT_AnmChg(wk->pAct[ACT_POT_T], ANM_P_OPEN);
			}
		}else if(wk->pot_open_f){
			wk->pot_open_f = FALSE;
			Snd_SePlay( SE_BC_CLOSE );
			CLACT_AnmChg(wk->pAct[ACT_POT_T], ANM_P_CLOSE);
		}
		return -1;
	}
//	CLACT_AnmChg(wk->pAct[ACT_POT_T], ANM_P_CLOSE);
	wk->pot_open_f = FALSE;
	if(pot_in){
		Snd_SePlay(SE_BC_PUT);
		return 1;
	}
	Snd_SePlay(SE_BC_RETURN);
	return 0;
}

/**
 *	@brief	ボール作成メイン選択入力取得
 */
int BCKey_InputBallMain(BC_APPWORK* wk)
{
	int ret;
	BOOL exe_f = FALSE;

	ret = touch_BallMain(wk,&exe_f);

	if(exe_f){
		return ret;
	}

	return key_BallMain(wk);
}

/**
 *	@brief	ボール作成個数入力取得
 */
int BCKey_InputBallNumSet(BC_APPWORK* wk)
{
	int ret;
	BOOL exe_f = FALSE;

	ret = touch_BallNumSet(wk,&exe_f);

	if(exe_f){
		return ret;
	}

	return key_BallNumSet(wk);
}

//////////////////////////////////////////////////////////////////////
//サブ

/*
 *	@brief	ノーマルモード作成メイン入力　実行処理
 */
static int exe_NormalMain(BC_APPWORK* wk,u32 code)
{
	if(code >= CURPOS_BONG01 && code <= CURPOS_BONG07){
		Snd_SePlay( SE_BC_DECIDE );
		BCSub_CursorPosSet(wk);
		wk->target = code;
		return SEQ_NORMAL_MAIN;
	}
	if(code == CURPOS_BACK){
		Snd_SePlay( SE_BC_CANCEL );
		BCSub_BackButtonWrite(wk,BTN_ANM);
		return SEQ_FADE_OUT;
	}
	return SEQ_NORMAL_MAIN;
}
/**
 *	@brief	ノーマルモード作成メイン選択入力　キー
 */
static int key_NormalMain(BC_APPWORK* wk)
{
	if(sys.trg & PAD_BUTTON_KTST_CHG){
		wk->key_mode = APP_KTST_KEY;
	}
	if(sys.trg & PAD_BUTTON_B){
		return exe_NormalMain(wk,CURPOS_BACK);
	}
	if(sys.trg & PAD_BUTTON_A){
		return exe_NormalMain(wk,BCKey_GetCursorPosNo(wk));
	}
	//カーソル移動検知
	if(keysub_MainMoveCPos(wk)){
		Snd_SePlay( SE_BC_SELECT );
		BCSub_CursorPosSet(wk);
		return SEQ_NORMAL_MAIN;
	}
	return SEQ_NORMAL_MAIN;
}

/**
 *	@brief	ノーマルモード作成メイン入力　タッチ
 */
static int touch_NormalMain(BC_APPWORK* wk,BOOL* exe_f)
{
	int	ret = 0;

	ret = GF_TP_HitTrg(TpMainRect);
	if(	ret == RECT_HIT_NONE || ret == CURPOS_POT){
		return SEQ_NORMAL_MAIN;
	}
	*exe_f = TRUE;
	wk->key_mode = APP_KTST_TOUCH;

	wk->opos_a = wk->opos_b = 0;
	wk->cpos_y = 0;
	switch(ret){
	case CURPOS_BACK:
		wk->cpos_x = CLINE_C;
		break;
	default:
		wk->cpos_x = ret%2;
		wk->cpos_y = ret/2;
	}
	return exe_NormalMain(wk,ret);
}

/*
 *	@brief	ベッド選択入力　実行処理
 */
static int exe_PotMain(BC_APPWORK* wk,u32 code)
{
	if(code >= CURPOS_BONG01 && code <= CURPOS_BONG07){
		BCSub_CursorPosSet(wk);
		if(wk->bong.num[code] == 0){
			return SEQ_POT_MAIN;
		}

		Snd_SePlay( SE_BC_CATCH );
		wk->target = code;
		if(wk->key_mode == APP_KTST_KEY){
			return SEQ_BONG_MOVE_KEY;
		}else{
			return SEQ_BONG_MOVE_TOUCH;
		}
	}
	if(code == CURPOS_BACK){
		Snd_SePlay( SE_BC_CANCEL );
		BCSub_BackButtonWrite(wk,BTN_ANM);
		return SEQ_FADE_OUT;
	}
	if(code == CURPOS_POT){
		Snd_SePlay( SE_BC_DECIDE );
		BCSub_CursorPosSet(wk);
		wk->target = code;

		//BTS2154の対応	3分岐	nagihashi
		if(wk->bong.juice_rest == 0 && wk->bong.stock_num == 0){
			//ドリンクがなくて、ストックもない
			BCSub_TalkMsgPut(wk,MIDX_TALK_JUICE_NONE,MSG_NO_PUT);
			BCSub_SetPassivePotMain(wk,BONG_STATE_PASSIVE);
			return SEQ_TALKMSG_WAIT;
		}else if(wk->bong.juice_rest == 0 && wk->bong.stock_num > 0){
			//ドリンクがなくて、ストックはある
			BCSub_TalkMsgPut(wk,MIDX_TALK_JUICE_CREATE,MSG_NO_PUT);
			BCSub_SetPassivePotMain(wk,BONG_STATE_PASSIVE);
			return SEQ_TALKMSG_WAIT;
		}else if(wk->bong.juice_rest > 0 && wk->bong.stock_num > 0){
			//ドリンクがあって、ストックもある
			BCSub_TalkMsgPut(wk,MIDX_TALK_JUICE_REFORM,MSG_NO_PUT);
			BCSub_SetPassivePotMain(wk,BONG_STATE_PASSIVE);
			return SEQ_TALKMSG_WAIT;
		}
		return SEQ_LIST_JUICE;
	}
	return SEQ_POT_MAIN;
}

/**
 *	@brief	ベッド選択入力　キー
 */
static int key_PotMain(BC_APPWORK* wk)
{
	if(sys.trg & PAD_BUTTON_KTST_CHG){
		wk->key_mode = APP_KTST_KEY;
	}
	if(sys.trg & PAD_BUTTON_B){
		return exe_PotMain(wk,CURPOS_BACK);
	}
	if(sys.trg & PAD_BUTTON_A){
		return exe_PotMain(wk,BCKey_GetCursorPosNo(wk));
	}
	//カーソル移動検知
	if(keysub_MainMoveCPos(wk)){
		Snd_SePlay( SE_BC_SELECT );
		BCSub_CursorPosSet(wk);
		return SEQ_POT_MAIN;
	}
	return SEQ_POT_MAIN;
}

/**
 *	@brief	ベッド選択入力　タッチ
 */
static int touch_PotMain(BC_APPWORK* wk,BOOL* exe_f)
{
	int	ret = 0;

	ret = GF_TP_HitTrg(TpMainRect);
	if(	ret == RECT_HIT_NONE){
		return SEQ_POT_MAIN;
	}
	*exe_f = TRUE;
	wk->key_mode = APP_KTST_TOUCH;

	wk->opos_a = wk->opos_b = 0;
	wk->cpos_y = 0;
	switch(ret){
	case CURPOS_POT:
		wk->cpos_x = CLINE_C;
		break;
	case CURPOS_BACK:
		wk->cpos_x = CLINE_D;
		break;
	default:
		wk->cpos_x = ret%2;
		wk->cpos_y = ret/2;
	}
	return exe_PotMain(wk,ret);
}

/*
 *	@brief	ボール作成メイン入力　実行処理
 */
static int exe_BallMain(BC_APPWORK* wk,u32 code)
{
	if(code >= CURPOS_BONG01 && code <= CURPOS_BONG07){
		Snd_SePlay( SE_BC_DECIDE );
		BCSub_CursorPosSet(wk);
		if(wk->bong.num[code] == 0){
			return SEQ_BALL_MAIN;
		}
		wk->target = code;
		wk->sel_num = 1;
		wk->sel_max = wk->bong.num[wk->target];
		return SEQ_BALL_NUMSET;
	}
	if(code == CURPOS_BACK){
		Snd_SePlay( SE_BC_CANCEL );
		BCSub_BackButtonWrite(wk,BTN_ANM);
		return SEQ_FADE_OUT;
	}
	return SEQ_BALL_MAIN;
}
/**
 *	@brief	ボール作成メイン選択入力　キー
 */
static int key_BallMain(BC_APPWORK* wk)
{
	if(sys.trg & PAD_BUTTON_KTST_CHG){
		wk->key_mode = APP_KTST_KEY;
	}
	if(sys.trg & PAD_BUTTON_B){
		return exe_BallMain(wk,CURPOS_BACK);
	}
	if(sys.trg & PAD_BUTTON_A){
		return exe_BallMain(wk,BCKey_GetCursorPosNo(wk));
	}
	//カーソル移動検知
	if(keysub_MainMoveCPos(wk)){
		Snd_SePlay( SE_BC_SELECT );
		BCSub_CursorPosSet(wk);
		return SEQ_BALL_MAIN;
	}
	return SEQ_BALL_MAIN;
}

/**
 *	@brief	ボール作成メイン入力　タッチ
 */
static int touch_BallMain(BC_APPWORK* wk,BOOL* exe_f)
{
	int	ret = 0;

	ret = GF_TP_HitTrg(TpMainRect);
	if(	ret == RECT_HIT_NONE || ret == CURPOS_POT){
		return SEQ_BALL_MAIN;
	}
	*exe_f = TRUE;
	wk->key_mode = APP_KTST_TOUCH;

	wk->opos_a = wk->opos_b = 0;
	wk->cpos_y = 0;
	switch(ret){
	case CURPOS_BACK:
		wk->cpos_x = CLINE_C;
		break;
	default:
		wk->cpos_x = ret%2;
		wk->cpos_y = ret/2;
	}
	return exe_BallMain(wk,ret);
}

/*
 *	@brief	ボール作成個数入力　実行処理
 */
static int exe_BallNumSet(BC_APPWORK* wk,u32 code)
{
	if(code == EXE_NSET_DECIDE){
		Snd_SePlay( SE_BC_DECIDE );
		BCSub_DCButtonWrite(wk,DC_BTN_DECIDE,BTN_ANM);
		return EXE_NSET_DECIDE;
	}
	if(code == EXE_NSET_CANCEL){
		Snd_SePlay( SE_BC_CANCEL );
		BCSub_DCButtonWrite(wk,DC_BTN_CANCEL,BTN_ANM);
		return EXE_NSET_CANCEL;
	}

	//BTS4935バグ修正　nagihashi
	switch(code){
	case EXE_NSET_UP1:
		wk->sel_num	= ItemNumMove(wk->sel_num, wk->sel_max, 1 );
		break;
	case EXE_NSET_UP10:
		wk->sel_num	= ItemNumMove(wk->sel_num, wk->sel_max, 10 );
		break;
	case EXE_NSET_DOWN1:
		wk->sel_num	= ItemNumMove(wk->sel_num, wk->sel_max, -1 );
		break;
	case EXE_NSET_DOWN10:
		wk->sel_num	= ItemNumMove(wk->sel_num, wk->sel_max, -10 );
		break;
	default:
		return EXE_NSET_NULL;
	}
	Snd_SePlay( SE_BC_SELECT );
	BCSub_BallSelectNumWrite(wk,wk->sel_num);
	return EXE_NSET_NULL;
}

//------------------------------------------------------------------
/**
 * @brief   どうぐの数と最大数の範囲で数を増減させる
 *
 * @param   num		現在の数
 * @param   max		最大数
 * @param   move	変動させる数（-100〜100）
 *
 * @retval  数
 *
 * bagのSHOP売り画面と完全に同じ動きをするために、bag_main.cからもってきました
 * nagihashi
 */
//------------------------------------------------------------------
static int ItemNumMove( int num, int max, int move )
{
	if(move>0){
		// プラスのとき
		if(num==max){
			num = 1;
		}else{
			num += move;
			if(num>max){
				num = max;
			}
		}
	}else{
		// マイナスのとき
		if(num==1){
			num = max;
		}else{
			num += move;
			if(num<=0){
				num = 1;
			}
		}
	}

	return num;
}

/**
 *	@brief	ボール作成個数入力　キー
 */
static int key_BallNumSet(BC_APPWORK* wk)
{
	if(sys.trg & PAD_BUTTON_KTST_CHG){
		wk->key_mode = APP_KTST_KEY;
	}
	if(sys.trg & PAD_BUTTON_B){
		return exe_BallNumSet(wk,EXE_NSET_CANCEL);
	}
	if(sys.trg & PAD_BUTTON_A){
		return exe_BallNumSet(wk,EXE_NSET_DECIDE);
	}
	if(sys.trg & PAD_KEY_UP){
		return exe_BallNumSet(wk,EXE_NSET_UP1);
	}
	if(sys.trg & PAD_KEY_DOWN){
		return exe_BallNumSet(wk,EXE_NSET_DOWN1);
	}
	if(sys.trg & PAD_KEY_RIGHT){
		return exe_BallNumSet(wk,EXE_NSET_UP10);
	}
	if(sys.trg & PAD_KEY_LEFT){
		return exe_BallNumSet(wk,EXE_NSET_DOWN10);
	}
	//カーソル移動検知
	if(sys.trg & PAD_KEY_DOWN){
		Snd_SePlay( SE_BC_SELECT );
		return EXE_NSET_NULL;
	}
	return EXE_NSET_NULL;
}

/**
 *	@brief	ボール作成個数入力　タッチ
 */
static int touch_BallNumSet(BC_APPWORK* wk,BOOL* exe_f)
{
	int	ret = 0;

	ret = GF_TP_HitTrg(TpBallNumSetRect);
	if(	ret == RECT_HIT_NONE){
		return EXE_NSET_NULL;
	}
	if(ret == EXE_NSET_UP10 || ret == EXE_NSET_DOWN10){
		if(wk->sel_max < 10){
			return EXE_NSET_NULL;
		}
	}
	if(ret >= EXE_NSET_UP1){
		CLACT_SetAnmFlag(wk->pAct[ACT_ALU01+(ret-EXE_NSET_UP1)],TRUE);
		CLACT_AnmReStart(wk->pAct[ACT_ALU01+(ret-EXE_NSET_UP1)]);
	}
	*exe_f = TRUE;
	wk->key_mode = APP_KTST_TOUCH;

	return exe_BallNumSet(wk,ret);
}

/**
 *	@brief	メイン　カーソルポジション取得
 */
static BOOL keysub_MainMoveCPos(BC_APPWORK* wk)
{
	switch(wk->cpos_x){
	case 0:	//Line A
		if(sys.trg & PAD_KEY_RIGHT){
			keysub_NextCPosLineAB(wk,0);
			return TRUE;
		}
		if(sys.trg & PAD_KEY_UP && wk->cpos_y > 0){
			wk->opos_b = 0;
			wk->cpos_y--;
			return TRUE;
		}
		if(sys.trg & PAD_KEY_DOWN && wk->cpos_y < 3){
			wk->opos_b = 0;
			wk->cpos_y++;
			return TRUE;
		}
		break;
	case 1:	//Line B
		if(sys.trg & PAD_KEY_LEFT){
			keysub_NextCPosLineAB(wk,1);
			return TRUE;
		}
		if(sys.trg & PAD_KEY_RIGHT){
			wk->opos_b = wk->cpos_y;
			wk->cpos_y = 0;
			wk->cpos_x++;
			return TRUE;
		}
		if(sys.trg & PAD_KEY_UP && wk->cpos_y > 0){
			wk->opos_a = 0;
			wk->cpos_y--;
			return TRUE;
		}
		if(sys.trg & PAD_KEY_DOWN && wk->cpos_y < 2){
			wk->opos_a = 0;
			wk->cpos_y++;
			return TRUE;
		}
		break;
	case 2:	//Line C
		if(sys.trg & PAD_KEY_LEFT){
			wk->cpos_y = wk->opos_b;
			wk->cpos_x--;
			return  TRUE;
		}
		if(wk->mode == SYSMODE_BALL){
			return FALSE;
		}
		if(sys.trg & (PAD_KEY_RIGHT|PAD_KEY_DOWN)){
			if(wk->mode!=SYSMODE_NORMAL){
				wk->cpos_x++;
				return TRUE;
			}
		}
		break;
	case 3:	//Line D
		if(sys.trg & (PAD_KEY_LEFT|PAD_KEY_UP)){
			wk->cpos_x--;
			return TRUE;
		}
		break;
	}
	return FALSE;
}

/**
 *	@brief	ポットメイン　カーソルポジション取得
 */
static void keysub_NextCPosLineAB(BC_APPWORK* wk,BOOL dir)
{
	if(dir == 0){	//AtoB
		wk->opos_a = wk->cpos_y;
		wk->cpos_x = 1;
		switch(wk->cpos_y){
		case 1:
		case 2:
			if((wk->cpos_y != wk->opos_b) && ((wk->cpos_y-1) != wk->opos_b)){
				wk->cpos_y = 1;
			}else{
				wk->cpos_y = wk->opos_b;
			}
			break;
		case 3:
			wk->cpos_y = 2;
			break;
		case 0:
		default:
			wk->cpos_y = 0;
			break;
		}
	}else{	//BtoA
		wk->opos_b = wk->cpos_y;
		wk->cpos_x = 0;
		switch(wk->cpos_y){
		case 0:
			if(wk->opos_a > 1){
				wk->cpos_y = 1;
			}else{
				wk->cpos_y = wk->opos_a;
			}
			break;
		case 1:
			if(wk->opos_a == 0){
				wk->cpos_y = 1;
			}else if(wk->opos_a == 3){
				wk->cpos_y = 2;
			}else{
				wk->cpos_y = wk->opos_a;
			}
			break;
		case 2:
			if(wk->opos_a < 2){
				wk->cpos_y = 2;
			}else{
				wk->cpos_y = wk->opos_a;
			}
			break;
		default:
			wk->cpos_y = 0;
			break;
		}
	}
}